// app.js
import data from 'utils/data.js';
App({
  onLaunch() {
    // 展示本地存储能力
    const logs = wx.getStorageSync('logs') || []
    logs.unshift(Date.now())
    wx.setStorageSync('logs', logs)
    // 登录
    var userAccount = wx.getStorageSync('userAccount')
    if (userAccount) { // 说明已登录 跳转 页面
      console.log("获取用户id：" + userAccount)
      wx.redirectTo({
        url: '/pages/home/home'
      })
    } else {// 未登录 显示 微信授权页面
      wx.login({
        success(res) {
          wx.login({
            success: res => {
              // 发送 res.code 到后台换取 openId, sessionKey, unionId
              if (res.code) {
                let code = res.code;
                wx.request({
                  url: data.commonUrl + '/api/v1/wx/getsessionkey',
                  data: {
                    'jscode': code,
                    'sign': 'sign'
                  },
                  method: 'GET',
                  header: {
                    'content-type': 'application/json'
                  }, // 设置请求的 header
                  success: function (data) {
                    console.log("openid->", data.data)
                    if (data.data == undefined) {
                      wx.showToast({
                        icon: "none",
                        title: 'openid获取失败，请重新登录！',
                      })
                      return
                    }
                    console.log("当前用户信息", data.data.openid)
                    wx.setStorageSync('userAccount', data.data.openid)
                    wx.redirectTo({
                      url: '/pages/home/home'
                    })
                  },
                  fail: function (err) {
                    console.log(err);
                    wx.showToast({
                      icon: "none",
                      title: 'openid获取失败，请重新登录！',
                    })
                  }
                })
              }
            }
          })
        }
      })
    }
  },
  globalData: {
    appid: "",
    appsecret: "",
    phoneNumber: '',
    userAccount: '',
    getSessionKeyUrl: data.commonUrl + '/api/v1/wx/getsessionkey',
    getPhoneUrl: data.commonUrl + '/api/v1/wx/getphone',
    getInfoReportListUrl: data.commonUrl + '/api/v1/infoReport/getInfoReportList',
    addInfoReportUrl: data.commonUrl + '/api/v1/infoReport/addInfoReport',
    updateInfoReportUrl: data.commonUrl + '/api/v1/infoReport/updateInfoReport',
    delInfoReportUrl: data.commonUrl + '/api/v1/infoReport/delInfoReport',
    getInfoReportByNo: data.commonUrl + '/api/v1/infoReport/getInfoReportByNo',
    getAreaDicInfo: data.commonUrl + '/api/v1/dict/getAreaDicInfo',
    getPcsDicInfo: data.commonUrl + '/api/v1/dict/getPcsDicInfo',
    getIndustryTypeDict: data.commonUrl + '/api/v1/dict/getIndustryTypeDict',
    uploadFile: data.commonUrl + '/api/v1/wx/uploadFile',
    addDataReport: data.commonUrl + '/api/v1/dataReport/add',
  }
})
